package com.zyt.common.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.SSOToken;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyt.brace.comm.pojo.Admin;
import com.zyt.brace.comm.service.AdminService;
import com.zyt.comm.beans.helper.SpringBeansManager;
import com.zyt.comm.core.support.controller.DateEditor;
import com.zyt.comm.core.support.controller.DoubleEditor;
import com.zyt.comm.core.support.controller.IntegerEditor;
import com.zyt.comm.util.RequestParamsUtil;
import com.zyt.comm.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 *@ClassName    BaseController.java
 *@module       基础类
 *@author       magl
 *@Date         2020年1月14日 下午1:41:09
 *@version      V1.0
 *@CopyRight    北京筑医台科技股份有限公司
 */
public class BaseController {
	
	@Autowired
	private AdminService adminService;

    /**
     * 缓存搜索条件的变量名，对应的值为URI的md5值
     * <br> ModeAndView.add(CACHE_SEARCH_ID_KEY, Md5(URI));
     */
	public static final String CACHE_SEARCH_ID_KEY = "cacheSearchId";

    /**
     * 是否缓存查询条件的关键字。
     */
	public static final String CACHE_SEARCH_FLAG = "isCacheSearchParams";

    /**
     * 查询条件缓存对象。
     */
    public static final String CACHE_SEARCH_PARAMS = "cacheSearchParams";

	/**
	 * 注入编辑器
	 * @param binder
	 */
	@InitBinder  
	public void initBinder(WebDataBinder binder){
		//日期编辑器
	    binder.registerCustomEditor(Date.class, new DateEditor());
	    //double类型数据编辑器
	    binder.registerCustomEditor(Double.class, new DoubleEditor());
	    //Integer类型数据编辑器
	    binder.registerCustomEditor(Integer.class, new IntegerEditor());
	}
	
	/**
	 * 获得登录用户信息
	 * @param request
	 * @return
	 */
	public Admin getLoginAdmin(HttpServletRequest request)throws Exception {
        Admin admin = null;
		SSOToken st = SSOHelper.getToken(request);
		if (st != null) {
			if(adminService == null){
				adminService = (AdminService) SpringBeansManager.getBean(AdminService.class);
			}
			admin = adminService.getAdmin(st.getUid());
			if (admin != null && !"".equals(admin.getAdminId())) {
				admin.setAdminPwd(null);
			}
		}
		return admin;
	}
	
	/**
	 * 获得登录用户ID
	 * @param request
	 * @return
	 */
	public String getLoginAdminId(HttpServletRequest request)throws Exception {
		String adminId = "";
		Admin admin = this.getLoginAdmin(request);
		if (admin != null && admin.getAdminId() != null) {
			adminId = admin.getAdminId();
		}
		return adminId;
	}
	
	/**
	 * 获得登录用户LoginID
	 * @param request
	 * @return
	 */
	public String getLoginAdminLoginId(HttpServletRequest request)throws Exception {
		String loginId = "";
		Admin admin = this.getLoginAdmin(request);
		if (admin != null && admin.getAdminId() != null) {
			loginId = admin.getLoginId();
		}
		return loginId;
	}

	/**
	 * 获取页面分页信息
	 * @param request
	 * @return
	 */
	public Page pageCovert(HttpServletRequest request) {
		Page page = new Page();
		page.setCurrent(1);
		page.setSize(15);

		String curPage = request.getParameter("page");
		if(NumberUtil.isInteger(curPage)){
			page.setCurrent(NumberUtil.parseInt(curPage));
		}
		String pageSizes = request.getParameter("rows");
		if(NumberUtil.isInteger(pageSizes)){
			page.setSize(NumberUtil.parseInt(curPage));
		}
		return page;
	}

    /**
     * 从Session取得缓存查询条件
     * @param request
     * @param mv
     */
	public void getCacheSearchParam(HttpServletRequest request, ModelAndView mv) {
        if (Boolean.valueOf(request.getParameter(CACHE_SEARCH_FLAG))) {
            String cacheSearchId = SecureUtil.md5(request.getRequestURI());
            mv.addObject(CACHE_SEARCH_PARAMS, request.getSession().getAttribute(cacheSearchId));
        }
        mv.addObject(CACHE_SEARCH_ID_KEY, SecureUtil.md5(request.getRequestURI()));
    }

    /**
     * 设置缓存查询条件到Session
     * @param request
     */
    public void setCacheSearchParam(HttpServletRequest request) {
        String cacheSearchId = request.getParameter(CACHE_SEARCH_ID_KEY);
        if (!StringUtils.isEmpty(cacheSearchId)) {
            request.getSession().setAttribute(cacheSearchId, RequestParamsUtil.getObjectParams(request));
        }
    }
}
